'use strict';

const Controller = require('egg').Controller;

/**
 * 人员去向表控制器
 */
class DestinationController extends Controller {
  /**
   * 用户登录
   * 验证手机号和密码，生成JWT token
   */
  async login() {
    const {
      ctx,
      app
    } = this;
    // 验证请求参数
    ctx.validate({
      phone_number: {
        type: 'string',
        required: true
      },
      password: {
        type: 'string',
        required: true
      },
    });

    const {
      phone_number,
      password
    } = ctx.request.body;

    // 验证用户凭据
    const person = await ctx.service.destination.findByPhoneAndPassword(phone_number, password);
    if (!person) {
      ctx.throw(422, '手机号或密码错误');
    }
    if (person.role_destination == '普通用户') {
      ctx.throw(422, '您没有权限')
    }

    // 生成包含用户信息的JWT token
    const token = app.jwt.sign({
      person_id: person.person_id,
      person_name: person.person_name,
    }, app.config.jwt.secret);

    // 返回token和用户基本信息
    ctx.body = {
      token,
      person_info: {
        person_id: person.person_id,
        person_name: person.person_name,
        role_destination: person.role_destination,
      },
    };
  }

  /**
   * 获取人员去向表
   * @param {string} department_name - 部门名称
   * @param {string} year_month - 年月
   * @returns {Promise<Object|null>} 人员去向表
   */
  async getDestination() {
    const {
      ctx
    } = this;

    ctx.validate({
      department_name: {
        type: 'string',
        required: false
      },
      year_month: {
        type: 'string',
        required: true
      },
    });

    const {
      department_name,
      year_month
    } = ctx.request.body;

    try {
      const result = await ctx.service.destination.getPersonDestinations({
        department_name,
        year_month
      });
      ctx.body = result;
    } catch (error) {
      ctx.throw(500, error);
    }
  }

  /**
   *  新增 / 更新 / 删除人员去向信息接口（统一处理）
   * @param {number} person_id - 人员id
   * @param {string} specific_date - YYYY-MM-dd
   * @param {string} destination  - 去向
   * @returns {Promise<Object|null>}
   */
  async handle() {
    const {
      ctx
    } = this;
    const {
      person_id,
      specific_date,
      destination
    } = ctx.request.body;

    try {
      const result = await ctx.service.destination.handlePersonDestination({
        person_id,
        specific_date,
        destination
      });
      ctx.status = 200;
      ctx.body = result;
    } catch (error) {
      ctx.throw(500, error)
    }
  }

  /**
   *  查询 某人 某天 的去向
   * @param {number} person_id - 人员id
   * @param {string} specific_date - YYYY-MM-dd
   * @returns {Promise<Object|null>}
   */
  async getDestinationByPidAndDate() {
    const {
      ctx
    } = this;
    const {
      person_id,
      specific_date,
    } = ctx.request.body;

    try {
      const result = await ctx.service.destination.handlePersonDestinationByPidAndDate({
        person_id,
        specific_date,
      });
      ctx.status = 200;
      ctx.body = result;
    } catch (error) {
      ctx.throw(500, error)
    }
  }

  /**
   *  查询 某人 所有去向
   * @param {number} person_id - 人员id
   * @returns {Promise<Object|null>}
   */
  async getDestinationByPid() {
    const {
      ctx
    } = this;
    const {
      person_id,
    } = ctx.request.body;

    try {
      const result = await ctx.service.destination.handlePersonDestinationByPid({
        person_id,
      });
      ctx.status = 200;
      ctx.body = result;
    } catch (error) {
      ctx.throw(500, error)
    }
  }

  /**
   * 查询 某人 所有投稿
   * @param {number} person_id - 人员id
   * @returns {Promise<Object|null>}
   */
  async getAllContributionByPid() {
    const {
      ctx
    } = this;
    const {
      person_id,
    } = ctx.request.body;

    try {
      const result = await ctx.service.destination.handlePersonContributionByPid({
        person_id,
      });
      ctx.status = 200;
      ctx.body = result;
    } catch (error) {
      ctx.throw(500, error)
    }
  }
}

module.exports = DestinationController